package com.ourgame.tankgame.control;

import com.jme3.asset.AssetManager;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.PhysicsTickListener;
import com.jme3.bullet.collision.PhysicsCollisionEvent;
import com.jme3.bullet.collision.PhysicsCollisionListener;
import com.jme3.bullet.collision.PhysicsCollisionObject;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.effect.ParticleEmitter;
import com.jme3.effect.ParticleMesh.Type;
import com.jme3.effect.shapes.EmitterSphereShape;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Spatial;
import com.ourgame.tankgame.behavior.LifeBehavior;
import com.ourgame.tankgame.behavior.PhysicsBehavior;

public class BulletControl extends RigidBodyControl implements PhysicsCollisionListener, PhysicsTickListener {

    protected PhysicsBehavior PhysicsBehavior;    
    private ParticleEmitter effect;
    private AssetManager assetManager;
    private float fxTime = 0.5f;
    private float maxTime = 4f;
    private float curTime = -1.0f;
    private float timer;

    public BulletControl(CollisionShape shape, float mass) {
        super(shape, mass);
    }

    public BulletControl(CollisionShape shape) {
        super(shape);
    }
    
    public BulletControl(CollisionShape shape, AssetManager assetManager, float mass) {
        this(shape, mass);
        this.assetManager = assetManager;
        prepareEffect(assetManager);
    }

    @Override
    public void setPhysicsSpace(PhysicsSpace space) {
        super.setPhysicsSpace(space);
        if (space != null) {
            space.addCollisionListener(this);

            setCollisionGroup(PhysicsCollisionObject.COLLISION_GROUP_03);
//            space.add(spatial);//将对于的物体加入到屋里系统

        }
    }

    @Override
    public void update(float tpf) {
        super.update(tpf);
        if (enabled) {
            timer += tpf;
            if (timer > maxTime) {
                if (spatial.getParent() != null) {
                    space.removeCollisionListener(this);
                    space.remove(this);
                    spatial.removeFromParent();
                }
            }
        }
        if (enabled && curTime >= 0) {
            curTime += tpf;
            if (curTime > fxTime) {
                curTime = -1;
                effect.removeFromParent();
            }
        }
    }

    public void playEffect() {
        if (effect != null && spatial.getParent() != null) {
            curTime = 0;
            effect.setLocalTranslation(spatial.getLocalTranslation());
            spatial.getParent().attachChild(effect);
            effect.emitAllParticles();
        }
    }

    private void prepareEffect(AssetManager assetManager) {
        int COUNT_FACTOR = 1;
        float COUNT_FACTOR_F = 1f;
        effect = new ParticleEmitter("Flame", Type.Triangle, 32 * COUNT_FACTOR);
        effect.setSelectRandomImage(true);
        effect.setStartColor(new ColorRGBA(1f, 0.4f, 0.05f, (float) (1f / COUNT_FACTOR_F)));
        effect.setEndColor(new ColorRGBA(.4f, .22f, .12f, 0f));
        effect.setStartSize(1.3f);
        effect.setEndSize(2f);
        effect.setShape(new EmitterSphereShape(Vector3f.ZERO, 1f));
        effect.setParticlesPerSec(0);
        effect.setGravity(0, -5f, 0);
        effect.setLowLife(.4f);
        effect.setHighLife(.5f);
        effect.setInitialVelocity(new Vector3f(0, 7, 0));
        effect.setVelocityVariation(1f);
        effect.setImagesX(2);
        effect.setImagesY(2);
        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Particle.j3md");
        mat.setTexture("Texture", assetManager.loadTexture("Effects/Explosion/flame.png"));
        effect.setMaterial(mat);
    }

    public void collision(PhysicsCollisionEvent event) {

        if (space == null) {
            return;
        }
        PhysicsCollisionObject objectA = event.getObjectA();
        PhysicsCollisionObject objectB = event.getObjectB();

        if (objectA == this || objectB == this) {
//            Vector3f bulletVec = getPhysicsLocation();
            space.addTickListener(this);
            playEffect();
            space.remove(this);
            spatial.removeFromParent();
        }

    }

    public void prePhysicsTick(PhysicsSpace ps, float f) {
        ps.removeCollisionListener(this);
    }

    public void physicsTick(PhysicsSpace ps, float f) {
//        spatial.removeFromParent();
        ps.removeTickListener(this);
//        ps.remove(this);

    }
}